package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"novel-data-hub/handlers"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}

func Init() *gin.Engine {
	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.POST("/login", handlers.Login)
	r.POST("/add", handlers.Add)
	r.GET("/list", handlers.List)
	r.GET("/detail", handlers.Detail)
	r.GET("/del", handlers.Del)

	/////////////////RBAC权限业务////////////
	r.POST("/user/userRoleAdd", handlers.UserRoleAdd)
	r.POST("/user/roleNodeAdd", handlers.RoleNodeAdd)
	r.GET("/user/userRoleList", handlers.UserRoleList)
	r.GET("/user/roleNodeList", handlers.RoleNodeList)

	/////////////////////车辆后台权限//////////////
	r.GET("/car/carNodeList", handlers.CarNodeList)     //车辆后台权限列表
	r.GET("/car/carNodeDetail", handlers.CarNodeDetail) //车辆后台权限详情

	// //////////////////车辆交易合同///////////////
	r.GET("/car/carContractList", handlers.CarContractList)     //车辆交易合同列表
	r.GET("/car/carContractDetail", handlers.CarContractDetail) //车辆交易合同详情

	// ///////////////车辆交易订单/////////////
	r.GET("/car/CarOrderList", handlers.CarOrderList)     //车辆交易订单列表
	r.GET("/car/CarOrderDetail", handlers.CarOrderDetail) //车辆交易订单详情

	////////////////////车辆数据信息管理//////////////////
	r.GET("/car/CarDataList", handlers.CarDataList)     //车辆数据信息列表
	r.GET("/car/CarDataDetail", handlers.CarDataDetail) //车辆数据信息详情

	//////////////////物流配送管理////////////////
	r.GET("/delivery/deliveryOrderList", handlers.DeliveryOrderList)     //物流配送管理列表
	r.GET("/delivery/deliveryOrderDetail", handlers.DeliveryOrderDetail) //物流配送管理详情

	// /////////////////物流仓库管理////////////////
	r.GET("/wareHouse/wareHouseManageList", handlers.WareHouseManageList)     //物流配送管理列表
	r.GET("/wareHouse/wareHouseManageDetail", handlers.WareHouseManageDetail) //物流配送管理详情

	// /////////////////物流后台权限/////////////////
	r.GET("/per/permissionList", handlers.PermissionList)     //物流后台权限列表
	r.GET("/per/permissionDetail", handlers.PermissionDetail) //物流后台权限详情

	// ///////////////////电商后台权限////////////////
	r.GET("/commerce/commercePerList", handlers.CommercePerList)     //电商后台权限列表
	r.GET("/commerce/commercePerDetail", handlers.CommercePerDetail) //电商后台权限详情
	return r
}
